﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for DataAccessHelper
/// </summary>
/// 
namespace BTGISWASECO.DataLayer
{
    public class DataAccessHelper
    {
        private DataTable _DataTableLayer;
        private string _LayerNames;

        public DataTable DataTableLayer
        {
            get { return _DataTableLayer; }
            set { _DataTableLayer = value; }
        }

        public string LayerNames
        {
            get { return _LayerNames; }
            set { _LayerNames = value; }
        }
        public DataAccessHelper()
        {

        }
        public DataTable GetDateTable()
        {
            System.Data.DataTable resultsDataTable = (DataTable)HttpContext.Current.Session["DataTable"];
            HttpContext.Current.Session["DataTable"] = null;
            return resultsDataTable;
        }

        public ESRI.ArcGIS.Server.IServerContext ServerDatabaseSDE()
        {
            
            ESRI.ArcGIS.Server.IServerContext serverContext = null;
            ESRI.ArcGIS.esriSystem.UID pUID = new ESRI.ArcGIS.esriSystem.UIDClass();
            string serverType = "MapServer";
            string serviceName = "BTGISWASECO";
            try
            {
                ESRI.ArcGIS.ADF.Identity identity = new ESRI.ArcGIS.ADF.Identity("Administrator", "", "localhost");
                ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection agsServerConnection;
                agsServerConnection =
                new ESRI.ArcGIS.ADF.Connection.AGS.AGSServerConnection("localhost", identity);
                agsServerConnection.Connect();
                ESRI.ArcGIS.Server.IServerObjectManager serverObjectManager = agsServerConnection.ServerObjectManager;
                serverContext = serverObjectManager.CreateServerContext(serviceName, serverType);

                #region thu get Map
                
               // ESRI.ArcGIS.Carto.IMapServer mapServer = serverContext.ServerObject as ESRI.ArcGIS.Carto.IMapServer;
               // //Log.Write("Server context established MapName=" + mapServer.DefaultMapName, "myclass");
               // ESRI.ArcGIS.Carto.IMapServerObjects mapSO = mapServer as ESRI.ArcGIS.Carto.IMapServerObjects;
               // //Log.Write("IMapServerObjects created", "myclass");
               // ESRI.ArcGIS.Carto.IMap map = mapSO.get_Map(mapServer.DefaultMapName);//(default)@BTGISWASECO
               //// Log.Write("Layers map established", "myclass");
               // //int layerIndex = -1;
               // //ESRI.ArcGIS.Carto.IFeatureLayer fLayer = map.get_Layer(0) as ESRI.ArcGIS.Carto.IFeatureLayer;
               // ESRI.ArcGIS.Carto.IEnumLayer el = map.get_Layers(null, true);
               // ESRI.ArcGIS.Carto.ILayer layer = null;
               // layer = el.Next();
               // //Loop through all the layers till it gets the Notification ( Jobs ) Spatial layer
               // while (layer != null)
               // {
               //     string ly=layer.Name;
               //     layer = el.Next();
               // }

                #endregion thu get Map


            }
            catch (System.Runtime.InteropServices.COMException comExc)
            {
                //System.Web.HttpResponse.Write("<script language=javascript>alert('Không kết nối được SDE')");
            }
            return serverContext;
        }
    }
}